//
//  SYWelcomVC.swift
//  SYWeibo
//
//  Created by Saya on 16/4/10.
//  Copyright © 2016年 黄金果. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class SYWelcomVC: UIViewController {

    override func loadView() {
        view = UIImageView(image: (UIImage(named: "ad_background")))
        setUpUI()
    }
    
    //viewdidLoad中的frame并不是真是的frame,所以需要使用viewDidAppear方法去设置frame
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)
        
        moveIconWithAnimation()
    }
    
    //使用动画的方法移动头像,让其从下面移动到上面
    private func moveIconWithAnimation (){
        
        /**
        usingSpringWithDamping:阻尼系数 0-1 ,阻尼系数 越大弹簧的感觉越小
        initialSpringVelocity:加速度 加速度越大,速度就会每秒增加的更快
        swift中的枚举: 使用[xxx,xxx] oc中的位枚举 XX | XX
        */
        //使用snapkit的update方法,不能添加新的约束,只能跟新约束,如果新的约束有冲突,否则会报错
        iconImg.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view.snp_bottom).offset(-580)
        }
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5, options: [], animations: { () -> Void in
                    self.view.layoutIfNeeded()
            }) { (finfished) -> Void in
                UIView.animateWithDuration(0.7, animations: { () -> Void in
                    
                })
                UIView.animateWithDuration(0.6, animations: { () -> Void in
                        self.userNameLb.layer.opacity = 0.8
                    }, completion: { (finished) -> Void in
                        //发起一个通知切换rootViewcontroller
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(SYSwitchRootController, object: "SYMainController")
                })
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置头像,afn虽然也有加载图片的功能,但是他没有将图片保存到沙盒中,只是 保存到内存中,最好使用sd,因为保存到沙盒中了
        //由于图片到处都需要使用,所以可以将图片获取url的代码放到ViewModel中,方便外界调用
        if (SYVIewModel.viewModel.userIconUrl?.absoluteString == ""){
            iconImg.image = UIImage(named: "avatar_default_big")
        }else{
            iconImg.sd_setImageWithURL(SYVIewModel.viewModel.userIconUrl)
        }
    
    }
    
    private func setUpUI (){
        view.addSubview(iconImg)
        view.addSubview(userNameLb)
    
        iconImg.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(view.snp_bottom).offset(-180)
            make.centerX.equalTo(view.snp_centerX)
        }
        
        userNameLb.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImg.snp_bottom).offset(16)
            make.centerX.equalTo(iconImg.snp_centerX)
        }
    }
    
    //MARK:懒加载各种控件
    lazy var iconImg:UIImageView = {
        let tempIcon = UIImageView()
        tempIcon.image = UIImage(named: "avatar_default_big")
        tempIcon.sizeToFit()
        //TODO:生成一个圆环头像
        tempIcon.layer.cornerRadius = tempIcon.frame.size.height * 0.5
        tempIcon.layer.masksToBounds = true
        return tempIcon
    }()
    
    lazy var userNameLb:UILabel = {
        let tempLb = UILabel()
        tempLb.text = SYVIewModel.viewModel.userInformation?.name
        tempLb.textColor = UIColor.darkGrayColor()
        tempLb.font = UIFont.systemFontOfSize(14)
        tempLb.sizeToFit()
        tempLb.layer.opacity = 0
        return tempLb
    }()
}















